package com.zhipu.balance.utils.upload.rapeseed

import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils
import com.zhipu.balance.listener.ChooseClickCallback
import com.zhipu.balance.mobile.ResultEntity
import com.zhipu.balance.mobile.event.*
import com.zhipu.balance.remotedata.InterceptObserver
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.dialog.CommonDialog
import com.zhy.http.okhttp.OkHttpUtils
import com.zhy.http.okhttp.callback.StringCallback
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.Call
import okhttp3.MediaType

/**
 * 油菜籽上传
 */
object RapeseedUploadUtils {

    /**
     * 外网上传油菜籽天平数据
     */
    fun uploadSoybeanBalanceInfo(mRecordRapeseed : RecordRapeseed, mChooseClickCallback : ChooseClickCallback?){
        var mNowMills = TimeUtils.getNowMills()
        var mGson = GsonUtils.toJson("${mRecordRapeseed}&${mNowMills}")
        var byteSign = InitializeUtils.getSignV0()?.signatureParams(mGson)
        var signature = ConvertUtils.bytes2HexString(byteSign)
        var mObservable = InitializeUtils.AppComponent()?.systemService?.uploadRapeseedBalanceInfo(signature,mNowMills,mRecordRapeseed)
        mObservable?.subscribeOn(Schedulers.io())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : InterceptObserver<String>() {
            /**
             * 成功数据data
             */
            override fun <T> onNextData(resultEntity: ResultEntity<T>?) {
                InitializeUtils.getDefaultMaker()?.show(resultEntity?.msg)
                mChooseClickCallback?.onChooseClickCallback(0)
                /**
                 * 关闭
                 */
                CommonDialog.dismissGP()
            }

            /**
             * 错误网络
             */
            override fun onErrorNet(t: Throwable?, netMsg: String?) {
                InitializeUtils.getDefaultMaker()?.show("上传失败")
                mChooseClickCallback?.onChooseClickCallback(1)
                /**
                 * 关闭
                 */
                CommonDialog.dismissGP()
            }

        })
    }


    /**
     * 内网上传油菜籽天平数据
     */
    fun IntranetRapeseedBalanceInfo(mRecordRapeseed : RecordRapeseed, mChooseClickCallback : ChooseClickCallback?){
        OkHttpUtils.postString()
            .url("http://"+ SPUtils.getInstance().getString("NetworkSettings","https://cg.zhipu-china.com/") + "/uploadRapeseedBalanceInfo")
            .content(GsonUtils.toJson(mRecordRapeseed))
            .mediaType(MediaType.parse("application/json; charset=utf-8")).build().execute(object : StringCallback() {
                override fun onError(call: Call?, e: Exception?, id: Int) {
                    InitializeUtils.getDefaultMaker()?.show("上传失败")
                    mChooseClickCallback?.onChooseClickCallback(1)
                    /**
                     * 关闭
                     */
                    CommonDialog.dismissGP()
                }
                override fun onResponse(response: String?, id: Int) {
                    var mResultEntity = GsonUtils.fromJson(response,ResultEntity::class.java)
                    when(mResultEntity.status){
                        200 ->{
                            InitializeUtils.getDefaultMaker()?.show(mResultEntity.msg)
                            mChooseClickCallback?.onChooseClickCallback(0)
                        }
                        else ->{
                            InitializeUtils.getDefaultMaker()?.show("上传失败")
                            mChooseClickCallback?.onChooseClickCallback(1)
                        }
                    }
                    /**
                     * 关闭
                     */
                    CommonDialog.dismissGP()
                }
            })
    }
}